package com.example.labSystem.service.impl;

import com.example.labSystem.entity.Attendance;
import com.example.labSystem.mapper.AttendanceMapper;
import com.example.labSystem.service.IAttendanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 考勤记录表 服务实现类
 * </p>
 *
 * @author 10
 * @since 2024-12-12
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements IAttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public List<Attendance> getAttendanceByUserID(String userID) {
        return attendanceMapper.selectByUserID(userID);
    }

    @Override
    public List<Attendance> listAttendance(int page, int size, String startDate, String endDate, String searchName) {
        // 设置分页
        Page<Attendance> attendancePage = new Page<>(page, size);

        // 设置查询条件
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();

        if (startDate != null && !startDate.isEmpty()) {
            queryWrapper.ge("checkInTime", startDate);//开始截止时间区间查询
        }
        if (endDate != null && !endDate.isEmpty()) {
            queryWrapper.le("checkOutTime", endDate);//开始截止时间区间查询
        }
        if (searchName != null && !searchName.isEmpty()) {
            queryWrapper.like("username", searchName); //username 查询
        }

        // 执行查询并返回分页结果
        return attendanceMapper.selectPage(attendancePage, queryWrapper).getRecords();
    }
}
